
import requests
from lxml import etree

url = 'https://www.pearvideo.com/category_9'
header = {
    'User-Agent': 'Mozilla/5.0(Windows NT 10.0;WOW64)AppleWebKit/537.36(KHTML, like Gecko) Chrome / 86.0.4240.198 Safari / 537.36'
}
zuikaishi = requests.get(url=url, headers=header)
zuikaishi.encoding = 'utf-8'
tree = etree.HTML(zuikaishi.text)
erjidizhi = tree.xpath('/html/body/div[2]/div[1]/div/ul/li/div/a/@href')
# print(erjidizhi)
for erji in erjidizhi:
    url2 = f'https://www.pearvideo.com/videoStatus.jsp?'
    param = {
        'contId': erji.split('_')[1],
        'mrd': '0.6150641064826337'
    }
    header = {
        'Referer': 'https://www.pearvideo.com/' + erji,
        'User-Agent': 'Mozilla/5.0(Windows NT 10.0;WOW64)AppleWebKit/537.36(KHTML, like Gecko) Chrome / 86.0.4240.198 Safari / 537.36'

    }
    sanjishuju = requests.get(url=url2, params=param, headers=header)
    sanjishuju.encoding = 'utf-8'
    sanjishuju = sanjishuju.json()
    tihuan = sanjishuju['systemTime']

    shipin_url = sanjishuju['videoInfo']['videos']['srcUrl']
    shipin_url = shipin_url.replace(tihuan, 'cont-' + erji.split('_')[1])
    shipin = requests.get(url=shipin_url, headers=header).content

    fp = open('./' + erji.split('_')[1] + '.mp4', 'wb')
    fp.write(shipin)
    print(erji.split('_')[1], '保存成功')

print('全部保存成功')
